<?php
class ControllerMyplanAllusers extends Controller{
	public function index(){
		
		$this->load->model("myplan/user");
		
		$query=array();
		
		$limit=10;
		
		if(isset($this->request->get["page"])){
			$page=$this->request->get["page"];
		}else{
			$page=1;
		}
		
		$query["start"]=($page-1)*$limit;
		$query["limit"]=$limit;
		
		$this->data["allusers"]=$this->model_myplan_user->getAllUsers($query);
		
				//分页
				$pagination = new Pagination();
				$pagination->total = $this->model_myplan_user->getTotalUsers();
				$pagination->page = $page;
				$pagination->limit = $limit;
				$pagination->text = "P";
				$pagination->url = $this->url->link('myplan/allusers',  $url . '&page={page}');
					
				$this->data['pagination'] = $pagination->render();
		
		$this->showuser();
	}	

	private function showuser(){
		$this->data["user_id"]=$this->user->getId();
	
		$allfriends=array();
		$allfriends=$this->model_myplan_user->getAllFriends($this->user->getId());
		$friendids=array();
		foreach($allfriends as $f){
			$friendids[]=$f['friend_id'];
		}
		$this->data["allfriends"]=$friendids;
		
		$this->data["checkplan"]=$this->url->link("myplan/checkplan")."&user_id=";
		$this->data["addfriend"]=$this->url->link("myplan/allusers/addfriend")."&user_id=";
		$this->data["action_searchuser"]=$this->url->link("myplan/allusers/searchuser");
	
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/myplan/allusers.tpl')) 		            {
				$this->template = $this->config->get('config_template') . '/template/myplan/allusers.tpl';
			} else {
				$this->template = 'default/template/myplan/allusers.tpl';
			}
			
			$this->children = array(
			'myplancommon/footer',
			'myplancommon/header'
			);
											
			$this->response->setOutput($this->render());
	}
	
	public function addfriend(){
		
		if(isset($this->request->get['user_id'])){
			$this->load->model("myplan/user");
			$data=array();
			$user_id=$this->user->getId();
			$friend_id=$this->request->get['user_id'];
			$data['from_user_id']=$user_id;
			$data['to_user_id']=$friend_id;
			$data['from_account']=$this->user->getNickname();
			if(!$this->model_myplan_user->hasRequest($data)){
				$this->model_myplan_user->addFriendRequest($data);
			}
			$this->session->data['success']="请求已发送";
			$this->redirect($this->url->link("myplan/allusers"));
		}
		
	}
	
	public function searchuser(){
	
		if(isset($this->request->post['searchusername'])){
			$this->load->model("myplan/user");
			$data=array();
			$data['nickname']=$this->request->post['searchusername'];
			$this->data["allusers"]=$this->model_myplan_user->getUserByNickname($data);
		}
		
		$this->showuser();
	}
}
?>
